﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;

namespace WebCrawler.ViewModel
{
    public class Web720yunAudioViewModel : ViewModelBase
    {
        public Web720yunAudioViewModel()
        {
            // 测试
            Url = "https://ssl-media.720static.com/@/3dfjOduakf3/1/bcfc22de7b6d91a4e5d299cdec0115c3.mp3";
            Name = "嘉兴南湖革命纪念";
        }

        #region 属性
        public string Url
        {
            get { return _url; }
            set { _url = value; RaisePropertyChanged(() => Url); }
        }
        private string _url;

        // 全景图名
        public string Name
        {
            get { return _name; }
            set { _name = value; RaisePropertyChanged(() => Name); }
        }
        private string _name;

        public string StepMessage
        {
            get { return _stepMessage; }
            set { _stepMessage = value; RaisePropertyChanged(() => StepMessage); }
        }
        private string _stepMessage;

        public bool IsDownloading
        {
            get { return _isDownloading; }
            set { _isDownloading = value; RaisePropertyChanged(() => IsDownloading); }
        }
        private bool _isDownloading = false;
        #endregion

        #region 命令
        public RelayCommand StartDownloadCmd
        {
            get
            {
                if (_startDownloadCmd == null)
                    _startDownloadCmd = new RelayCommand(startDownload, canStartDownload);
                return _startDownloadCmd;
            }
            set { _startDownloadCmd = value; }
        }
        public RelayCommand _startDownloadCmd;
        #endregion

        #region 辅助函数
        private async void startDownload()
        {
            //string folderPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}/{Name}";
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}/{Name}";

            StepMessage = "开始";
            IsDownloading = true;

            await DownloadAudioAsync(Url, folderPath);

            IsDownloading = false;
            StepMessage = "完成";
        }

        private bool canStartDownload()
        {
            return true;
        }

        public async System.Threading.Tasks.Task DownloadAudioAsync(string url, string folderPath)
        {
            HttpHelper.HttpItem item = new HttpHelper.HttpItem()
            {
                URL = url,
                Method = "GET",
                ContentType = "audio/mp3",
                Accept = "*/*",
                ResultType = HttpHelper.ResultType.Byte,
                Referer = "https://720yun.com/"
            };

            await System.Threading.Tasks.Task.Run(() =>
            {
                HttpHelper.HttpHelper http = new HttpHelper.HttpHelper();
                HttpHelper.HttpResult result = http.GetHtml(item);

                if (result != null && result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    if (!string.IsNullOrEmpty(folderPath))
                    {
                        if (!System.IO.Directory.Exists(folderPath))
                        {
                            System.IO.Directory.CreateDirectory(folderPath);
                        }

                        System.IO.File.WriteAllBytes($"{folderPath}/{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.mp3", result.ResultByte);
                    }
                }
                else { }
            });
        }
        #endregion

        public void ReleaseRegister()
        {
            Messenger.Default.Unregister(this);
        }
    }
}